package com.mf.controller;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mf.dao.EslesmeDAO;
import com.mf.model.Eslesme;
import com.mf.model.Kullanici;
import com.mf.service.EslesmeService;
import com.mf.service.KullaniciService;

@Controller
public class MatchingController {

	@Autowired
	EslesmeService eslesmeService;

	@Autowired
	EslesmeDAO eslesmeDAO;

	@Autowired
	KullaniciService kullaniciService;

	private static final Logger logger = LoggerFactory.getLogger(MatchingController.class);

	@RequestMapping(value = MatchingRestURIConstants.GET_MATCH_BY_USER, method = RequestMethod.GET)
	public @ResponseBody
	List<Eslesme> getMatchesByUser(@PathVariable("id") int fromId) {
		Kullanici kullanici = (Kullanici) kullaniciService.findById(Kullanici.class, fromId);
		List<Eslesme> fromEeslesmeList = (List<Eslesme>) eslesmeService
				.findByProperty(Eslesme.class, "from", kullanici);
		List<Eslesme> toEslesmeList = (List<Eslesme>) eslesmeService.findByProperty(Eslesme.class, "to", kullanici);
		List<Eslesme> allList = fromEeslesmeList;

		for (Eslesme eslesme : toEslesmeList) {
			Kullanici tempFrom = eslesme.getFrom();
			eslesme.setFrom(eslesme.getTo());
			eslesme.setTo(tempFrom);
			allList.add(eslesme);
		}

		return allList;

		// for (Dinleme dinleme2 : dinlemes) {
		// dinleme2.setBitisTarih(Calendar.getInstance());
		// dinlemeService.saveOrUpdate(dinleme2);
		// }
		// List<Eslesme> eslesmeList = eslesmeDAO.getEslesmeListByFromId(fromId);
		// return eslesmeList;
	}

	@RequestMapping(value = MatchingRestURIConstants.GET_MATCH_BY_ID, method = RequestMethod.GET)
	public @ResponseBody
	Eslesme getMatchById(@PathVariable("id") int id) {
		Eslesme eslesme = (Eslesme) eslesmeService.findById(Eslesme.class, id);
		return eslesme;
	}

	@RequestMapping(value = MatchingRestURIConstants.CREATE_MATCH, method = RequestMethod.POST)
	public @ResponseBody
	Eslesme createMatch(@RequestBody Eslesme eslesme) {
		eslesme.setTarih(new Date());
		eslesmeService.save(eslesme);
		return eslesme;
	}

	@RequestMapping(value = MatchingRestURIConstants.DELETE_MATCH, method = RequestMethod.GET)
	public @ResponseBody
	Eslesme deleteMatch(@PathVariable("id") int eslesmeId) {
		Eslesme eslesme = (Eslesme) eslesmeService.findById(Eslesme.class, eslesmeId);
		eslesmeService.delete(eslesme);
		return eslesme;
	}

	@RequestMapping(value = MatchingRestURIConstants.UPDATE_MATCH, method = RequestMethod.GET)
	public @ResponseBody
	Eslesme updateMatch(@PathVariable("id") int eslesmeId) {
		Eslesme eslesme = (Eslesme) eslesmeService.findById(Eslesme.class, eslesmeId);
		eslesme.setFlag(true);
		eslesmeService.saveOrUpdate(eslesme);
		return eslesme;
	}

}